import { Injectable } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { TypeOrmModuleOptions, TypeOrmOptionsFactory } from '@nestjs/typeorm';
import { Category } from '../category/entities/category.entity';
import { Role } from '../role/entities/role.entity';
import { AdminUser } from '../admin-users/entities/admin-user.entity';
import { Permission } from '../permission/entities/permission.entity';
import { Menu } from '../menu/entities/menu.entity';
import { RolePermission } from '../role-permission/entities/role-permission.entity';
import { RoleMenu } from '../role-permission/entities/role-menu.entity';

@Injectable()
export class TypeOrmConfigService implements TypeOrmOptionsFactory {
	constructor(private configService: ConfigService) {}

	createTypeOrmOptions(): TypeOrmModuleOptions {
		return {
			type: this.configService.get('database.type'),
			url: this.configService.get('database.url'),
			host: this.configService.get('database.host'),
			port: this.configService.get('database.port'),
			username: this.configService.get('database.username'),
			password: this.configService.get('database.password'),
			database: this.configService.get('database.name'),
			synchronize: this.configService.get('database.synchronize'),
			dropSchema: false,
			keepConnectionAlive: true,
			logging: this.configService.get('app.nodeEnv') !== 'production',
			entities: [Category, Role, AdminUser, Permission, Menu, Role, RolePermission, RoleMenu],
			migrations: [__dirname + '/migrations/**/*{.ts,.js}'],
			cli: {
				entitiesDir: 'src',
				migrationsDir: 'src/database/migrations',
				subscribersDir: 'subscriber',
			},
			extra: {
				// based on https://node-postgres.com/apis/pool
				// max connection pool size
				max: this.configService.get('database.maxConnections'),
				ssl: this.configService.get('database.sslEnabled')
					? {
							rejectUnauthorized: this.configService.get('database.rejectUnauthorized'),
							ca: this.configService.get('database.ca') ?? undefined,
							key: this.configService.get('database.key') ?? undefined,
							cert: this.configService.get('database.cert') ?? undefined,
					  }
					: undefined,
			},
		} as TypeOrmModuleOptions;
	}
}
